package com.azt.provider.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.azt.api.dto.GiftInfoData;
import com.azt.api.pojo.GiftExchange;
import com.azt.api.pojo.GiftInfo;
import com.azt.api.pojo.GiftStock;
import com.azt.api.pojo.UserAccount;
import com.azt.api.service.GiftService;
import com.azt.api.service.UserService;
import com.azt.model.page.Pagination;
import com.azt.provider.mapper.GiftExchangeMapper;
import com.azt.provider.mapper.GiftInfoMapper;
import com.azt.provider.mapper.GiftStockMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
@Transactional
public class GiftServiceImpl implements GiftService {
	
	@Autowired
	private GiftInfoMapper giftInfoMapper;
	
	@Autowired
	private GiftStockMapper giftStockMapper;
	
	@Autowired
	private GiftExchangeMapper giftExchangeMapper;
	
	@Autowired
	private UserService userService;
	
	@Override
	public Pagination giftInfoList(int pageno, int pagesize, Map<String, String> params) {
		Page<Object> startPage = PageHelper.startPage(pageno, pagesize);
		List<GiftInfo> giftInfoList=giftInfoMapper.giftInfoList(params);
		Pagination p = new Pagination(startPage, giftInfoList);
		p.setList(giftInfoList);
		p.setParams(params);
		return p;
	}

	@Override
	public String getGiftCode() {
		Integer id=giftInfoMapper.getMaxIdFormGiftInfo();
		StringBuffer sb=new StringBuffer("NJ");
		String idstr="";
		if(id==null){
			idstr="1";
		}else{
			id+=1;
			idstr=id.toString();
		}
		for (int i = 0; i < 6-idstr.length(); i++) {
			sb.append("0");
		}
		sb.append(idstr);
		return sb.toString();
	}

	@Override
	public void addGiftInfo(GiftInfo giftInfo) {
		giftInfoMapper.insertSelective(giftInfo);
	}

	@Override
	public void updateGiftInfo(GiftInfo giftInfo) {
		giftInfoMapper.updateByPrimaryKeySelective(giftInfo);
	}

	@Override
	public GiftInfo getGiftInfoById(Integer id) {
		return giftInfoMapper.selectByPrimaryKey(id);
	}

	@Override
	public void plChangeGiftState(String ids) {
		giftInfoMapper.plChangeGiftState(ids);
	}

	@Override
	public void plDeletegiftInfos(String ids) {
		giftInfoMapper.plDeleteGiftInfo(ids);
	}

	@Override
	public void updateGiftStateOne(String id) {
		giftInfoMapper.updateGiftStateOne(id);
	}

	@Override
	public List<GiftStock> getGiftListByGid(Integer gid) {
		GiftStock gs=new GiftStock();
		gs.setGiftId(gid);
		return giftStockMapper.select(gs);
	}

	@Override
	public void addGiftStock(GiftStock stock) {
		giftStockMapper.insertSelective(stock);
		GiftInfo g=giftInfoMapper.selectByPrimaryKey(stock.getGiftId());
		g.setStockAmount(g.getStockAmount()+stock.getAmount());
		giftInfoMapper.updateByPrimaryKeySelective(g);
	}

	@Override
	public Pagination giftExchangePage(int pageno, int pagesize, Map<String, String> params) {
		Page<Object> startPage = PageHelper.startPage(pageno, pagesize);
		List<GiftExchange> giftExList=giftExchangeMapper.selectGiftExchangeList(params);
		Pagination p = new Pagination(startPage, giftExList);
		p.setList(giftExList);
		p.setParams(params);
		return p;
	}

	@Override
	public void updateExchange(GiftExchange exchange) {
		giftExchangeMapper.updateByPrimaryKeySelective(exchange);
	}

	@Override
	public List<GiftExchange> getExportChange(String state) {
		return giftExchangeMapper.selectExportExchange(state);
	}
	
	public List<GiftInfoData> getGiftInfoData(Integer minPoint, Integer maxPoint, Integer state ,Integer exchangeState) {
		return giftInfoMapper.getGiftInfoData(minPoint,maxPoint,state,exchangeState);
	}

	@Override
	public Pagination searchGift(int pageNo, int pageSize, Map<String, String> searchMap) {
		Integer minPoint = null;
		Integer maxPoint = null;
		Integer state = 1;
		Integer exchangeState = null;
		if(StringUtils.isNotBlank(searchMap.get("so_minPoint"))){
			minPoint=Integer.parseInt(searchMap.get("so_minPoint"));
			if(minPoint.equals(-1) || minPoint.equals(-2)){
				minPoint=null;
			}
		}
		if(StringUtils.isNotBlank(searchMap.get("so_maxPoint"))){
			maxPoint=Integer.parseInt(searchMap.get("so_maxPoint"));
			if(maxPoint.equals(-1)){
				maxPoint=null;
			}else if(maxPoint.equals(-2)){
				UserAccount userAccount = userService.getUserAccountByUserId(Integer.parseInt(searchMap.get("userId")));
				maxPoint=userAccount.getUsIntegral();
			}
		}
		if(StringUtils.isNotBlank(searchMap.get("so_state"))){
			state=Integer.parseInt(searchMap.get("so_state"));
		}
		if(StringUtils.isNotBlank(searchMap.get("so_exchangeState"))){
			state=Integer.parseInt(searchMap.get("so_exchangeState"));
		}
		Page<Object> startPage = PageHelper.startPage(pageNo, pageSize);
		List<GiftInfoData> giftInfoDataList = this.getGiftInfoData(minPoint,maxPoint,state,exchangeState);
		Pagination p = new Pagination(startPage, giftInfoDataList);
		p.setParams(searchMap);
		return p;
	}

	@Override
	public GiftInfo selectCanExchangeMax(Integer userId) {
		return giftInfoMapper.selectCanExchangeMax(userId);
	}

	@Override
	public GiftInfo selectCanNotExchangeMin(Integer userId) {
		return giftInfoMapper.selectCanNotExchangeMin(userId);
	}

	@Override
	public List<GiftInfoData> selectHotGifts(Integer offset,Integer limit,Integer state){
		return giftInfoMapper.selectHotGifts(offset,limit,state);
	}
	
	@Override
	public GiftInfoData selectGiftInfoDetail(Integer giftId){
		return giftInfoMapper.selectGiftInfoDetail(giftId);
	}
	
	@Override
	public void addGiftExchange(GiftExchange giftExchange){
		giftExchangeMapper.insertSelective(giftExchange);
	}
	
	@Override
	public void updateGiftStock(GiftExchange ge){
		giftInfoMapper.updateGiftStock(ge.getGiftId(),ge.getExchangeAmount());
	}

    @Override
    public int getMinIntegralAmount() {
        return giftInfoMapper.getMinIntegralAmount();
    }
}
